/**
 * 
 */
package com.cea.papyrus.diagram.component.figure;

import org.eclipse.draw2d.FreeformLayer;
import org.eclipse.draw2d.FreeformLayout;
import org.eclipse.draw2d.FreeformViewport;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.ScrollPane;
import org.eclipse.draw2d.StackLayout;
import org.eclipse.draw2d.ToolbarLayout;
import org.eclipse.uml2.uml.Element;

import com.cea.papyrus.core.PapyrusPlugin;
import com.cea.papyrus.core.figures.NodeFigure;
import com.cea.papyrus.core.helper.ModelerHelper;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;

/**
 * Defines the view of an artifact.
 * 
 * @author al090761
 *
 */
public class ArtifactFigure extends BasicArtifactFigure{

	/**
	 * display the content of the package
	 * *.
	 */
	protected class ArtifactContentFigure extends NodeFigure  {
	
		/**
		 * 
		 */
		private IFigure pane;
		
		/**
		 * 
		 */
		protected ScrollPane scrollpane;
	
		/**
		 * 
		 */
		public ArtifactContentFigure() {
			// Set content layout
			setLayoutManager(new StackLayout());
	
			// Add pane for content
			pane = new FreeformLayer();
			pane.setLayoutManager(new FreeformLayout());
	
			// Add scrollpane
			ScrollPane scrollpane = new ScrollPane();
			scrollpane.setViewport(new FreeformViewport());
			scrollpane.setContents(pane);
			add(pane);
		}
	
		/**
		 * 
		 * 
		 * @return 
		 */
		public IFigure getContentPane() {
			return pane;
		}
	
		/**
		 * Paint border.
		 * 
		 * @param graphics the graphics
		 */
		@Override
		public void paintBorder(Graphics graphics){	}
	}

	/**
	 * 
	 */
	protected ArtifactContentFigure contentFigure;

	/**
	 * The Constructor.
	 * 
	 * @param keyword 
	 */
	public ArtifactFigure(String keyword){
		super(keyword);
		setOpaque(true);
		ToolbarLayout layout = new ToolbarLayout();
		setLayoutManager(layout);			
		contentFigure =new ArtifactContentFigure();
		add(contentFigure);		
	}

	/**
	 * Use local coordinates.
	 * 
	 * @return true, if use local coordinates
	 */
	@Override
	protected boolean useLocalCoordinates(){return true;}

	/**
	 * 
	 * 
	 * @return 
	 */
	public IFigure getContentPane() {
		return contentFigure.getContentPane();	
	}
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.common.figures.IAbstractElementFigure#refreshPresentation(java.lang.String, org.eclipse.uml2.uml.Element)
	 */
	/**
	 * Refresh presentation.
	 * 
	 * @param presentation the presentation
	 * @param selection the selection
	 */
	@Override
	public void refreshPresentation(String presentation, Element selection,GraphElement ge) {
		org.eclipse.swt.graphics.Image icon =PapyrusPlugin.getIconElement(selection);
	
		/* if the presentation is icon or icon with text, we can set the icon */
		if ( (icon != null) 
			&& (presentation.equals(ModelerHelper.ICON_STEREOTYPE_PRESENTATION)
				 || presentation.equals(ModelerHelper.TEXT_ICON_STEREOTYPE_PRESENTATION)) ) {
			this.setIcon(icon);
			if (iconLabel == null) {
				createIconLabel();
			}
			iconLabel.setIcon(icon);
			iconLabel.repaint();
			this.add(iconLabel,1);
			this.repaint();		
		}
		else if (presentation.equals(ModelerHelper.DEFAULT_PRESENTATION) 
				|| presentation.equals(ModelerHelper.TEXT_STEREOTYPE_PRESENTATION)) {
//			setIcon(PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"ArtifactFile_16x16.gif"));
			setIcon(PapyrusPlugin.getImage(PapyrusPlugin.PAPYRUS_ICONS_16x16+"ArtifactFile_16x16.jpg"));
		}
		else {
			if (iconLabel != null) {
				iconLabel.setIcon(null);
				iconLabel.repaint();
				if (iconLabel.getParent() != null)	{
					this.remove(iconLabel);
				}
				this.repaint();
			}
		}
		return;
	}
}
